package org.sninwo.lnbook.book.service;

import java.util.List;

import javax.annotation.Resource;

import org.sninwo.lnbook.book.dao.UserDao;
import org.sninwo.lnbook.book.entity.User;
import org.sninwo.lnbook.uitls.LangUtils;
import org.sninwo.lnbook.uitls.WebUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional
public class UserManager {
	@Resource
	private UserDao userDao;
	
	public List<User> getUsers(){
		return userDao.getAll();
	}
	
	public User getUserByName(String name){
		if (name == null)
			return null;
		return userDao.findUniqueBy("username", name);
	}
	
	/**
	 * 修改当前登陆用户的密码
	 * @param newPassword
	 */
	public void changePassword(String newPassword){
		User user = WebUtils.getLoginInfo();
		user.setPassword(LangUtils.md5(newPassword));
		userDao.save(user);
	}
}
